package com.leyou.user.controller;

import com.leyou.user.pojo.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @author:zty
     * @date: 2019年01月22日 14:20:44
     * @description  校验数据是否可用
     * @method  checkUserData
     * @param data
     * @param type
     * @return org.springframework.http.ResponseEntity<java.lang.Boolean>
     */
    @GetMapping("check/{data}/{type}")
    public ResponseEntity<Boolean> checkUserData(@PathVariable("data") String data, @PathVariable(value = "type") Integer type){
        Boolean boo=this.userService.checkData(data,type);
        if (boo == null){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.ok(boo);
    }
   /**
     * @author:zty
     * @date: 2019年01月22日 14:20:13
     * @description 生成并发送手机验证码
     * @method  sendVerifyCode
     * @param phone
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    @PostMapping("code/{phone}")
    public ResponseEntity<Void> sendVerifyCode(@PathVariable("phone") String phone) {
        Boolean boo = this.userService.sendVerifyCode(phone);
        if (boo == null || !boo) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    /**
     * @author:zty
     * @date: 2019年01月22日 14:26:52
     * @description
     * @method  register
     * @param user
     * @param code
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code){
        Boolean boo = this.userService.register(user,code);
        if (boo==null|| !boo){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return  new ResponseEntity<>(HttpStatus.CREATED);
    }
    /**
     * @author:zty
     * @date: 2019年01月22日 16:49:29
     * @description 验证登录信息
     * @method  QueryUserByUserNameByPassWord
     * @param username
     * @param password
     * @return org.springframework.http.ResponseEntity<com.leyou.user.pojo.User>
     */
//    @GetMapping("query")
//    public ResponseEntity<User>QueryUserByUserNameByPassWord(@RequestParam("username") String username,@RequestParam("password")String password){
//        User user = this.userService.queryUserByUsernameAndPassword(username,password);
//        System.out.println(user);
//        if (user==null){
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//        }
//        return  new ResponseEntity<>(HttpStatus.CREATED);
//    }

    /**
     * 根据用户名和密码查询用户
     * @param username
     * @param password
     * @return
     */
    @GetMapping("query")
    public ResponseEntity<User> queryUser(@RequestParam("username") String username, @RequestParam("password") String password) {
        return ResponseEntity.ok(userService.queryUser(username, password));
    }

}